package love.yuanmengrobot.qdkaoqin.business;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.adapter.ClazzAdapter;
import love.yuanmengrobot.qdkaoqin.adapter.MemberTAdapter;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.entity.Member;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TeacherClazzDescActivity extends AppCompatActivity {
    private String CLAZZ_ID;
    private String CLAZZ_NAME;
    private String MEMBER_ID;
    ///////
    private ListView lvMemberList;
    private List<Member> memberData;
    private MemberTAdapter memberAdapter;
    private String urlClazziddParams;
    private Button kaoqingButton;
    ///////

    private TextView tvClazzName;
    private TextView tvClazzId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.clazz_desc);
        Intent intent = getIntent();
        CLAZZ_ID = intent.getStringExtra(TeacherConstants.CLAZZ_ID);
        urlClazziddParams = "?clazzid=" + CLAZZ_ID;

        CLAZZ_NAME = intent.getStringExtra(TeacherConstants.CLAZZ_NAME);
        MEMBER_ID = intent.getStringExtra(TeacherConstants.MEMBERID);
        System.out.println("班级id：" + CLAZZ_ID);
        System.out.println("班级名称：" + CLAZZ_NAME);
        System.out.println("*用户ID：" + MEMBER_ID);
        initView();
        initDate();
    }


    ///////////
    private void initView() {
        lvMemberList = findViewById(R.id.list_desc_member);
//        lvMemberList.setOnItemClickListener(
//                new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> adapterView,
//                                            View view, int i, long l) {
//                        Intent intent = new Intent(TeacherMainActivity.this,
//                                TeacherClazzDescActivity.class);
//                        Member member = memberAdapter.getItem(i);
//                        intent.putExtra(TeacherConstants.CLAZZ_ID,
//                                member.getClazzid());
//                        startActivity(intent);
//                    }
//                });
        tvClazzName = findViewById(R.id.tv_clazz_name_desc);
        tvClazzId = findViewById(R.id.tv_clazz_id_desc);
        tvClazzName.setText("班级：" + CLAZZ_NAME);
        tvClazzId.setText("编号：" + CLAZZ_ID);
        kaoqingButton = findViewById(R.id.bu_kaoqin);
        kaoqingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TeacherClazzDescActivity.this,
                        KaoqinActivity.class);
                intent.putExtra(TeacherConstants.CLAZZ_ID,
                        CLAZZ_ID);
                intent.putExtra(TeacherConstants.CLAZZ_NAME,
                        CLAZZ_NAME);
                intent.putExtra(TeacherConstants.MEMBERID,
                        MEMBER_ID);
                startActivity(intent);

            }
        });

    }

    public void initDate() {
        memberData = new ArrayList<>();
        memberAdapter = new MemberTAdapter(TeacherClazzDescActivity.this,
                R.layout.list_clazz_desc_member_item, memberData);
        lvMemberList.setAdapter(memberAdapter);
        refreshData(1);
    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =////////****
                                new TypeToken<List<Member>>() {
                                }.getType();
                        List<Member> memberListResponse =
                                gson.fromJson(body, jsonType);
                        if(memberListResponse==null)return;
                        for (Member me : memberListResponse) {
                            memberAdapter.add(me);
                        }
                        memberAdapter.notifyDataSetChanged();
                    }
                });
            } else {

            }
        }
    };


    private void refreshData(final int page) {
//?memberid=11
        new Thread(new Runnable() {
            @Override
            public void run() {

                Request request = new Request.Builder()
                        .url(TeacherConstants.CLAZZ_DESC_URL + urlClazziddParams)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }


}